<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>json</title>
</head>
<body>
<script>
    //1. 定义一个json对象 => 具备一定格式的对象
    //{key1:value1,key2:value2}
    let obj = {"id":1,"username":"admin"};
    //通过json对象的key获取到对应的value
    console.log(obj.username);

    //2. 定义一个json数组[包含很多个json对象]
    let arr = [{"id":1,"username":"admin"}
        ,{"id":2,"username":"tom"}
        ,{"id":3,"username":"jack"}];
    console.log(arr[1].username);

    //3. 复杂一点的json的对象
    let result={"code":200,"msg":"查询商品信息","data":[
            {"id":1,"username":"admin"},
            {"id":2,"username":"tom"},
            {"id":3,"username":"jack"}
        ]}
    console.log(result.data[2].username)

    //4. 定义一个json字符串
    let json_str = '{"id":1,"username":"admin"}';
    //json字符串转换成json对象
    let json_str_obj = JSON.parse(json_str);
    console.log(json_str_obj.username);

    //5. json对象转成json字符串的.
    let result_str = JSON.stringify(result,null,"\t");
    console.log(result); // json对象
    console.log(result_str); // json字符串
</script>
</body>
</html>